package com.yiwei.hdb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.UploadTypeAdapter;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.view.Dialog;
import com.yiwei.hdb.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3UploadTypeActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    private Unbinder unbinder;
    private UploadTypeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_type_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("选择上传类型");
        adapter = new UploadTypeAdapter(this);
        adapter.setOnChooseListener(new UploadTypeAdapter.OnChooseListener() {
            @Override
            public void onClick(String can_post, String id, String type) {
                if(can_post.equals("0")){
                    Dialog.showExtensionLimitDialog(V3UploadTypeActivity.this, new Dialog.DialogClickListener() {
                        @Override
                        public void confirm() {

                        }
                    });
                    return;
                }
                Intent intent = new Intent(V3UploadTypeActivity.this, V3UploadThingsActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("type", type);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        });
        list.setAdapter(adapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getSpreadFtype();
    }

    public void getSpreadFtype() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        loadingDialog.show();

        Api.api_service.getSpreadFtype().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.setCan_post(result.data.can_post);
                    adapter.update(result.data.lists);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
